package ru.ipo.dces.problemsviewer;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 09.06.11
 * Time: 21:37
 */
//TODO find out how to set nornal cells width and height
public class HistoryTable extends JTable {

    public HistoryTable(ProblemSubmissionHistory history) {
        TableModel model = history.getTableModel();
        setModel(model);
        model.addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                if (e.getType() == TableModelEvent.INSERT) {//no other events are interesting
                    int firstRow = e.getFirstRow();
                    if (firstRow == TableModelEvent.HEADER_ROW)
                        return;
                    int lastRow = e.getLastRow();
                    if (lastRow == TableModelEvent.HEADER_ROW) //not sure this is possible
                        return;
                    for (int row = firstRow; row <= lastRow; row++)
                        updateRowHeight(row);
                }
            }
        });

        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                updateAllRowsHeight();
                updateColHeight(0);
                updateColHeight(1);
                doLayout();
            }
        });
    }

    private void updateAllRowsHeight() {
        for (int i = 0; i < getRowCount(); i++)
            updateRowHeight(i);
    }

    private void updateRowHeight(int row) {
        //update rows height
        int cols = getColumnCount();
        int prefSize = 0;
        for (int i = 0; i < cols; i++) {
            TableCellRenderer renderer = getCellRenderer(row, i);
            Dimension preferredSize = renderer.getTableCellRendererComponent(
                    this,
                    getModel().getValueAt(row, i),
                    false,
                    false,
                    row,
                    i
            ).getPreferredSize();

            int psh = preferredSize.height;
//            if (preferredSize.width < getColumn(getColumnName(i)).getWidth())
//                psh *= 3;

            if (psh > prefSize)
                prefSize = psh;
        }
        setRowHeight(row, prefSize);
    }

    private void updateColHeight(int col) {
        int rows = getRowCount();
        int prefSize = 0;
        for (int i = 0; i < rows; i++) {
            TableCellRenderer renderer = getCellRenderer(i, col);
            Dimension preferredSize = renderer.getTableCellRendererComponent(
                    this,
                    getModel().getValueAt(i, col),
                    false,
                    false,
                    i,
                    col
            ).getPreferredSize();

            int psw = preferredSize.width;

            if (psw > prefSize)
                prefSize = psw;
        }
        getColumn(getColumnName(col)).setPreferredWidth(prefSize + 2);
    }
}
